import { defineStore } from 'pinia';
import { useInitStore } from '@/hooks';
import { I_LoginRes } from '@/types';
export default defineStore("user", {
    state: (): { init_state: I_LoginRes | null; user: I_LoginRes; } => {
        return {
            user: {
                id: 0,
                menus: [],
                name: "",
                password: "",
                role: "",
                token: "",
                uid: "",
            },
            init_state: null,
        };
    },
    getters: {
    },
    actions: {
        initState(): boolean {
            if (this.init_state) return false;

            const RawState: any = toRaw(this);
            for (let item of Object.keys(RawState)) {
                if (!/(^\$|^_|init_state)/.test(item) && typeof RawState[item] !== "function") {
                    if (!this.init_state) this.init_state = {} as I_LoginRes;
                    (this as any).init_state[item] = useInitStore<any>(toRaw(RawState[item]));
                }
            }
            return true;
        },
        resetState(): boolean {
            if (!this.init_state) return false;
            const RawState: any = toRaw(this.init_state);
            for (let item of Object.keys(RawState)) {
                if (!/(^\$|^_|init_state)/.test(item) && typeof RawState[item] !== "function") {
                    (this as any)[item] = reactive(useInitStore<any>(toRaw(RawState[item])));
                }
            }
            return true;
        },
    }
});
